package com.csot.service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.csot.model.TotalSegment;
import com.google.common.collect.Lists;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.NumberUtil;

public class Combine {

	public static List<String> combine(List<String> list) {
		//拆分-
		List<String> paramList = Lists.newArrayList();
		for(String f : list) {
			for (String str : f.split("-")) {
				paramList.add(str);
			}
		}
		//排序
		List<Long> orderList = paramList.stream().map(f -> {
			Long i = 0L;
			if(NumberUtil.isLong(f)) {
				i = Long.parseLong(f);
			} else {
				i = NetUtil.ipv4ToLong(f);
			}
			return i;
		}).sorted().collect(Collectors.toList());
		//整理成链表
		List<Long[]> lists = Lists.newArrayList();
		for(int x = 0; x < orderList.size(); x=x+2) {
			Long[] longs = new Long[2];
			longs[0] = orderList.get(x);
			longs[1] = orderList.get(x+1);
			lists.add(longs);
		}
		//逻辑处理
		List<String[]> ipList = Lists.newArrayList();
		long last = 0L;
		
		for(int i = 0; i < lists.size(); i++) {
			String[] strs = new String[2];
			Long[] values = lists.get(i);
			if(values[0] - last <= 1 ) {
				ipList.get(ipList.size() - 1)[1] = NetUtil.longToIpv4(values[1]);
			} else {
				strs[0] = NetUtil.longToIpv4(values[0]);
				strs[1] = NetUtil.longToIpv4(values[1]);
				ipList.add(strs);
				last = values[1];
			}
		}
		//整理成返回格式
		List<String> resList = Lists.newArrayList();
		for(int j = 0; j < ipList.size(); j++) {
			resList.add(ipList.get(j)[0] + "-" + ipList.get(j)[1]);
		}
		
		return resList;
	}
	
	
	public static List<String> combineNew(List<String> list) {
		//拆分-
		List<combineVo> paramList = Lists.newArrayList();
		for(String f : list) {
			String[] strs = f.split("-");
			//开始
			combineVo vo1 = new combineVo();
			Long i = 0L;
			if(NumberUtil.isLong(strs[0])) {
				i = Long.parseLong(strs[0]);
			} else {
				i = NetUtil.ipv4ToLong(strs[0]);
			}
			vo1.setIp(i);
			vo1.setFlag("start");
			paramList.add(vo1);
			//结束
			combineVo vo2 = new combineVo();
			Long j = 0L;
			if(NumberUtil.isLong(strs[1])) {
				j = Long.parseLong(strs[1]);
			} else {
				j = NetUtil.ipv4ToLong(strs[1]);
			}
			vo2.setIp(j);
			vo2.setFlag("end");
			paramList.add(vo2);
		}
		//排序
		List<combineVo> orderList = paramList.stream().sorted(Comparator.comparing(combineVo::getIp)).collect(Collectors.toList());
		//逻辑处理
		List<String> ipList = Lists.newArrayList();
		long last = 0L;
		String flag = "";
		for(int i = 0; i < orderList.size(); i++) {
			combineVo values = orderList.get(i);
			if("start".equals(flag)) {
				if("start".equals(values.getFlag())) {
					last = values.getIp();
					flag = values.getFlag();
				} else {
					last = values.getIp();
					flag = values.getFlag();
					ipList.add(NetUtil.longToIpv4(values.getIp()));
				}
			} else if("end".equals(flag)) {
				if("start".equals(values.getFlag())) {
					if(values.getIp() - last <= 1 ) {
						ipList.remove(ipList.size() - 1);
						ipList.add(NetUtil.longToIpv4(values.getIp()));
					} else {
						last = values.getIp();
						flag = values.getFlag();
						ipList.add(NetUtil.longToIpv4(values.getIp()));
					}
					
				} else {
					last = values.getIp();
					flag = values.getFlag();
					ipList.remove(ipList.size() - 1);
					ipList.add(NetUtil.longToIpv4(values.getIp()));
				}
				
			} else {
				last = values.getIp();
				flag = values.getFlag();
				ipList.add(NetUtil.longToIpv4(values.getIp()));
			}
		}
		
		//整理成返回格式
		List<String> resList = Lists.newArrayList();
		for(int j = 0; j < ipList.size(); j=j+2) {
			resList.add(ipList.get(j) + "-" + ipList.get(j+1));
		}
		
		return resList;
	}
	
	public static class combineVo {

		Long ip;

		String flag;

		public Long getIp() {
			return ip;
		}

		public void setIp(Long ip) {
			this.ip = ip;
		}

		public String getFlag() {
			return flag;
		}

		public void setFlag(String flag) {
			this.flag = flag;
		}
	}
	
//	public static void main(String[] args) {
//		List<String> list = Lists.newArrayList();
//		list.add("192.168.0.1-192.168.2.11");
//		list.add("192.168.0.1-192.168.0.6");
//		list.add("192.168.0.5-192.168.0.10");
//		list.add("192.168.0.12-192.168.0.15");
//		list.add("192.168.2.1-192.168.2.8");
//		list.add("192.168.2.9-192.168.2.10");
//		list.add("192.168.2.12-192.168.2.13");
//		
//		List<String> l = combineNew(list);
//		
//		l.forEach(f -> {
//			System.out.println(f);
//		});
//	}
}
